#define _CRT_SECURE_NO_WARNINGS

#include "Sort.h"

void PrintArray(int* a, int n)
{
	assert(a);

	for (int i = 0; i < n; i++)
	{
		printf("%d ", a[i]);
	}
	printf("\n");
}

void swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void InsertSort(int* a, int n)
{
	assert(a);

	for (int i = 1; i < n; i++)
	{
		int end = i - 1;
		int tmp = a[i];
		while (end >= 0)
		{
			if (a[end] > tmp)
			{
				swap(&a[end], &a[end + 1]);
				end--;
			}
			else break;
		}
		a[end + 1] = tmp;
	}
}

void SherSort(int* a, int n)
{
	assert(a);

	int gap = n;
	while (gap > 1)
	{
		gap /= 2;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (a[end] > tmp)
				{
					swap(&a[end], &a[end + gap]);
					end -= gap;
				}
				else break;
			}
			a[end + gap] = tmp;
		}
	}
}

void SelectSort(int* a, int n)
{
	assert(a);

	int l = 0, r = n - 1;
	while (l < r)
	{
		int mini = l, maxi = l;
		for (int i = l; i <= r; i++)
		{
			if (a[i] > a[maxi])
			{
				maxi = i;
			}
			if (a[i] < a[mini])
			{
				mini = i;
			}
		}
		swap(&a[l], &a[mini]);
		if (maxi == l)
		{
			maxi = mini;
		}
		swap(&a[r], &a[maxi]);

		l++;
		r--;
	}
}

void adjustdown(int* a, int n, int parent)
{
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			child++;
		}
		if (a[child] > a[parent])
		{
			swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
void HeapSort(int* a, int n)
{
	assert(a);

	for (int i = (n - 2) / 2; i >= 0; i--)
	{
		adjustdown(a, n, i);
	}

	int end = n - 1;
	while (end > 0)
	{
		swap(&a[0], &a[end]);
		adjustdown(a, end, 0);
		end--;
	}
}

void BubbleSort(int* a, int n)
{
	assert(a);

	for (int i = 0; i < n - 1; i++)
	{
		bool flag = true;

		for (int j = 1; j < n - i; j++)
		{
			if (a[j - 1] > a[j])
			{
				swap(&a[j - 1], &a[j]);
				flag = false;
			}
		}

		if (flag)
		{
			break;
		}
	}
}

int mid_num(int* a, int l, int r)
{
	int mid = (l + r) / 2;
	if (a[l] > a[r])
	{
		if (a[r] > a[mid])
		{
			return r;
		}
		else if (a[l] < a[mid])
		{
			return l;
		}
		else
		{
			return mid;
		}
	}
	else
	{
		if (a[l] > a[mid])
		{
			return l;
		}
		else if (a[r] < a[mid])
		{
			return r;
		}
		else
		{
			return mid;
		}
	}
}
void QuickSort(int* a, int l, int r)
{
	if (l >= r) return;

	if (r - l + 1 < 15)
	{
		InsertSort(a + l, r - l + 1);
	}
	else
	{
		int mid = QuickSort11(a, l, r);

		QuickSort(a, l, mid - 1);
		QuickSort(a, mid + 1, r);
	}
}
void QuickSort1(int* a, int l, int r)
{
	if (l >= r)
	{
		return;
	}

	int mid = mid_num(a, l, r);
	swap(&a[l], &a[mid]);
	int keyi = l, begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= a[keyi]) end--;
		while (begin < end && a[begin] <= a[keyi]) begin++;

		swap(&a[begin], &a[end]);
	}
	swap(&a[keyi], &a[begin]);

	QuickSort1(a, l, begin - 1);
	QuickSort1(a, begin + 1, r);
}
void QuickSort2(int* a, int l, int r)
{
	if (l >= r)
	{
		return;
	}

	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int key = a[l], hole = l;
	int begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= key) end--;
		a[hole] = a[end];
		hole = end;

		while (begin < end && a[begin] <= key) begin++;
		a[hole] = a[begin];
		hole = begin;
	}
	a[hole] = key;

	QuickSort2(a, l, hole - 1);
	QuickSort2(a, hole + 1, r);
}
void QuickSort3(int* a, int l, int r)
{
	if (l >= r)
	{
		return;
	}

	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int keyi = l, prev = l, cur = l + 1;

	while (cur <= r)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
			swap(&a[prev], &a[cur]);

		cur++;
	}
	swap(&a[prev], &a[keyi]);

	QuickSort3(a, l, prev - 1);
	QuickSort3(a, prev + 1, r);
}
int QuickSort11(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[l], &a[mid]);
	int keyi = l, begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= a[keyi]) end--;
		while (begin < end && a[begin] <= a[keyi]) begin++;

		swap(&a[begin], &a[end]);
	}
	swap(&a[keyi], &a[begin]);

	return begin;
}
int QuickSort22(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int key = a[l], hole = l;
	int begin = l, end = r;

	while (begin < end)
	{
		while (begin < end && a[end] >= key) end--;
		a[hole] = a[end];
		hole = end;

		while (begin < end && a[begin] <= key) begin++;
		a[hole] = a[begin];
		hole = begin;
	}
	a[hole] = key;

	return hole;
}
int QuickSort33(int* a, int l, int r)
{
	int mid = mid_num(a, l, r);
	swap(&a[mid], &a[l]);
	int keyi = l, prev = l, cur = l + 1;

	while (cur <= r)
	{
		if (a[cur] < a[keyi] && ++prev != cur)
			swap(&a[prev], &a[cur]);

		cur++;
	}
	swap(&a[prev], &a[keyi]);

	return prev;
}
//void QuickSortNonR(int* a, int l, int r)
//{
//	Stack st;
//	StackInit(&st);
//
//	STPush(&st, r);
//	STPush(&st, l);
//
//	while (!STEmpty(&st))
//	{
//		int begin = STTop(&st);
//		STPop(&st);
//		int end = STTop(&st);
//		STPop(&st);
//
//		int mid = QuickSort11(a, begin, end);
//
//		if (mid + 1 < end)
//		{
//			STPush(&st, end);
//			STPush(&st, mid + 1);
//		}
//		if (mid - 1 > begin)
//		{
//			STPush(&st, mid - 1);
//			STPush(&st, begin);
//		}
//	}
//
//	STDestroy(&st);
//}

//void MergeSortY(int* a, int l, int r, int* tmp)
//{
//	if (l >= r) return;
//
//	int mid = (l + r) / 2;
//
//	MergeSortY(a, l, mid, tmp);
//	MergeSortY(a, mid + 1, r, tmp);
//
//	int i = l, j = mid + 1;
//	int k = l;
//
//	while (i <= mid && j <= r)
//	{
//		if (a[i] < a[j])
//		{
//			tmp[k++] = a[i++];
//		}
//		else
//		{
//			tmp[k++] = a[j++];
//		}
//	}
//	while (i <= mid)
//	{
//		tmp[k++] = a[i++];
//	}
//	while (j <= r)
//	{
//		tmp[k++] = a[j++];
//	}
//
//	memcpy(a + l, tmp + l, sizeof(int) * (r - l + 1));
//}
//void MergeSort(int* a, int n)
//{
//	assert(a);
//
//	int* tmp = (int*)malloc(sizeof(int) * n);
//	assert(tmp);
//
//	MergeSortY(a, 0, n - 1, tmp);
//}

// again
void QuickSortNonR(int* a, int l, int r)
{
	Stack st;
	StackInit(&st);

	STPush(&st, r);
	STPush(&st, l);

	while (!STEmpty(&st))
	{
		int begin = STTop(&st);
		STPop(&st);
		int end = STTop(&st);
		STPop(&st);

		int mid = QuickSort11(a, begin, end);

		if (mid + 1 < end)
		{
			STPush(&st, end);
			STPush(&st, mid + 1);
		}
		if (mid - 1 > begin)
		{
			STPush(&st, mid - 1);
			STPush(&st, begin);
		}
	}

	STDestroy(&st);
}

void MergeSortY(int* a, int l, int r, int* tmp)
{
	if (l >= r) return;

	int mid = (l + r) / 2;

	MergeSortY(a, l, mid, tmp);
	MergeSortY(a, mid + 1, r, tmp);

	int i = l, j = mid + 1;
	int k = l;
	while (i <= mid && j <= r)
	{
		if (a[i] < a[j])
		{
			tmp[k++] = a[i++];
		}
		else
		{
			tmp[k++] = a[j++];
		}
	}
	while (i <= mid)
	{
		tmp[k++] = a[i++];
	}
	while (j <= r)
	{
		tmp[k++] = a[j++];
	}

	memcpy(a + l, tmp + l, sizeof(int) * (r - l + 1));
}
void MergeSort(int* a, int n)
{
	assert(a);

	int* tmp = (int*)malloc(sizeof(int) * n);
	assert(tmp);

	MergeSortY(a, 0, n - 1, tmp);
}